--This is my latest release of the Sierra-Style engine written in Lingo.
--January 6, 1998
--Please, have a look at the read me file included in this archive for information on using
--this code.
--NEW FEATURES
--============
--Smooth-moving sprites (no more jagginess when moving - goes in straight path)
--Unpassable boundaries (Sprites that you set up that stop the character sprite)
--HOW IT WORKS
--============
--Basically you click the mouse, it gets the position of the click and the sprite, and then
--calculates the path that the sprite has to move. I've also gotten code in there that allows
--for boundaries that the sprite stops at even if the mouse click was beyond the boundary.
--The calculations are made by using the positions to get the horizontal and vertical distances
--from the sprite to the mouse-click destination. These are then used to divide each other
--and come up with ratios that the sprite moves along the x and y axises (or whatever the plural
--for axis is) until it hits either the destination or a boundary. Then it stops.
--PROBLEMS/FUTURE UPDATES
--=======================
--There is one feature I plan to add and one bug I plan to fix. The feature is when the sprite
--hits a boundary it will slide along the boundary until it's x or y coordinate is the same as
--the mouse or it hits a corner which is as far as it can go towards the mouse. I have already
--made a bit of progress with this (not in this release, but my next one). The bug is minor -
--the fact that as the sprite speeds up it can't get as close to the boundary as when it is slow.
--This is not a major problem, but it can get annoying. I may have to rethink the boundary
--check to accomplish this.
global mhorz --The horizontal position of the mouse
global mvert --Vertical pos of mouse
global hdist --Horizontal distance for sprite to move
global vdist --Vertical dist for sprite to move
global svert --Vertical pos of sprite
global shorz --Horiz pos of sprite
global hratio --Horiz ratio of mouse pos to sprite pos
global vratio --Vert ratio
global hdir --Horiz direction for sprite to move
global vdir --Vert dir
global movetype --Used in previous release
global ismoving --Whether the sprite is moving or not
global t1var --Used in previous release
global t3var --Used in previous release
global speed --Speed of movement
global bratio --Border slant ratio
global sratio --Sprite ratio compared to borders
global cando --Whether the mouse can continue moving (or hit a boudary)
global btype --What type of boundary (which slant direction, and is it taller or longer)
global bsprite --The sprite nums that are borders
on startmovie
set speed to 10 --The number of pixels the sprite makes per jump
set the puppet of sprite 1 to true --Moveable by lingo
set ismoving to false --Starts off not moving
end
on idle
if ismoving = true then calcpath --If its moving then calculate the path of movement
end idle
on mousedown
put the mousev into mvert --Get the vertical pos of the mouseclick
put the mouseh into mhorz --Get the horiz pos
put the locv of sprite 1 into svert --Get the vertical location of the sprite
put the loch of sprite 1 into shorz --Get the horiz pos
calcpath --Calculate the path of movement
updatestage --Update the stage with new sprite locations
end
on calcpath
put abs(mvert - svert) into vdist --The vertical distance from click to sprite
put abs(mhorz - shorz) into hdist --The horiz distance
if float(hdist) <> 0 then
put float(vdist) / float(hdist) into vratio --The vertical dist ratio
else
put 0 into vratio
end if
if float(vdist) <>0 then
put float(hdist) / float(vdist) into hratio --The horiz dist ratio
else
set hratio to 0
end if
if hratio > vratio then
set hratio to 1
set vratio to vratio / hratio --*
else
set vratio to 1
set hratio to hratio / vratio --*
end if
--*We want one ratio to be equal to 1 and the other to be equal to less than one so that the
--sprite moves at equal speeds no matter where you click.
if mvert <= svert and mhorz >= shorz then
set hdir to speed
set vdir to -speed
end if
if mvert >= svert and mhorz <= shorz then
set hdir to -speed
set vdir to speed
end if
if mvert >= svert and mhorz >= shorz then
set hdir to speed
set vdir to speed
end if
if mvert <= svert and mhorz <= shorz then
set hdir to -speed
set vdir to -speed
end if
--I just set if the sprite is moving up or down and left or right on the screen
set ismoving to true --So it calls calcpath again
set the castNum of sprite 1 to 1 --Animated while moving
moveit --Changes the position of the sprite
end
on moveit
set shorz to shorz + (hratio * hdir) --Moving it horizontally
set svert to svert + (vratio * vdir) --Moving it vertically
set ismoving to true --So it calls calcpath again
set the loch of sprite 1 to shorz --Changing the actual horiz pos
set the locv of sprite 1 to svert --Changing the actual vert pos
checkb --Checks for boundaries
updatestage --Updates with new sprite locations
set cando to true --Next move is boundary free by default
checkdone --Checks if it has reached its destination
end
on checkdone
if (shorz < mhorz + speed and shorz > mhorz - speed) and (svert < mvert + speed ¨
and svert > mvert - speed) then
--If the sprite location is equal to the mouse location give or take the speed
set shorz to mhorz --Sets the sprite to the click location exactly
set svert to mvert --Ditto
stopmoving --Sets everything back to standstill
end if
end
on stopmoving
set the loch of sprite 1 to shorz - (hratio * hdir) --In case it overshot a boundary
set the locv of sprite 1 to svert - (vratio * vdir) --Ditto
set ismoving to false --So it doesnt call calcpath anymore
set the castNum of sprite 1 to 3 --Back to non-animated
updatestage --New sprite location
end
--This is the part that checks boundaries. The way it works is the boundaries are sprites of
--horizontal lines, one from upper right to lower left, the other from upper left to lower
--right. The ratios don't work correctly both when the sprite is stretched taller than wider
--and vice versa, so there has to be two sets of code for each sprite.
on checkb
repeat with bsprite = 7 to 10 --The sprites that are boundaries
if the castnum of sprite bsprite = 26 then
if the width of sprite bsprite > the height of sprite bsprite then
--This boundary is wider than taller (width>height) and slants right-left (castnum 26)
put float(the width of sprite bsprite) / float(the height of sprite bsprite) into bratio
if (shorz - the right of sprite bsprite) / bratio > svert - the bottom of sprite bsprite - speed and (shorz - the right of sprite bsprite) / bratio < svert - the bottom of sprite bsprite + speed and shorz > the left of sprite bsprite and shorz < the right of sprite bsprite then
--Just checked if the sprite hit the boundary
stopmoving --Stops the sprite
end if
else
--This boundary is taller than wide not (width>height) and slants right-left (castnum 26)
put float(the height of sprite bsprite) / float(the width of sprite bsprite) into bratio
if (svert - the bottom of sprite bsprite) / bratio > shorz - the right of sprite bsprite - speed and (svert - the bottom of sprite bsprite) / bratio < shorz - the right of sprite bsprite + speed and svert > the top of sprite bsprite and svert < the bottom of sprite bsprite then
--Just checked if the sprite hit the boundary
stopmoving --Stops the sprite
end if
end if
else
if the width of sprite bsprite > the height of sprite bsprite then
--This boundary is wider than taller (width>height) and slants left-right not(castnum 26)
put float(the width of sprite bsprite) / float(the height of sprite bsprite) into bratio
if -(shorz - the right of sprite bsprite) / bratio > svert - the top of sprite bsprite - speed and -(shorz - the right of sprite bsprite) / bratio < svert - the top of sprite bsprite + speed and shorz > the left of sprite bsprite and shorz < the right of sprite bsprite then
--Just checked if the sprite hit the boundary
stopmoving --Stops the sprite
end if
else
--This boundary is taller than wide not(width>height) and slants
--left-right not(castnum 26)
put float(the height of sprite bsprite) / float(the width of sprite bsprite) into bratio
if -(svert - the top of sprite bsprite) / bratio > shorz - the right of sprite bsprite - speed and -(svert - the top of sprite bsprite) / bratio < shorz - the right of sprite bsprite + speed and svert > the top of sprite bsprite and svert < the bottom of sprite bsprite then
--Just checked if the sprite hit the boundary
stopmoving --Stops the sprite
end if
end if
end if
end repeat
end
--That's it. If you have any ideas on how to improve the code, any bug reports etc., please
--send them to me at rudis@mindless.com. If you have not yet done so, please read the read me
--file that was included with this script for information on distributing this package or